{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Reverse Vowels of a String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #two-pointers #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #双指针 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: reverseVowels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #反转字符串中的元音字母"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串 <code>s</code> ，仅反转字符串中的所有元音字母，并返回结果字符串。</p>\n",
    "\n",
    "<p>元音字母包括 <code>'a'</code>、<code>'e'</code>、<code>'i'</code>、<code>'o'</code>、<code>'u'</code>，且可能以大小写两种形式出现不止一次。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"hello\"\n",
    "<strong>输出：</strong>\"holle\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"leetcode\"\n",
    "<strong>输出：</strong>\"leotcede\"</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>\n",
    "\t<li><code>s</code> 由 <strong>可打印的 ASCII</strong> 字符组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [reverse-vowels-of-a-string](https://leetcode.cn/problems/reverse-vowels-of-a-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [reverse-vowels-of-a-string](https://leetcode.cn/problems/reverse-vowels-of-a-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"hello\"', '\"leetcode\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        # vowels of a string: a, e, i, o, u\n",
    "        # in the first time, forget the upperletter....\n",
    "        # too slow\n",
    "        # key_list, value_list = [], []\n",
    "        # for i in range(len(s)):\n",
    "        #     if s[i] in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']:\n",
    "        #         key_list.append(i)\n",
    "        #         value_list.append(s[i])\n",
    "        # s = list(s)\n",
    "        # for j in range(len(key_list)):\n",
    "        #     s[key_list[j]] = value_list[::-1][j]\n",
    "        # return \"\".join(s)\n",
    "    \n",
    "        i,j,v,a=0,len(s)-1,set(\"aeouiAEOUI\"),list(s)\n",
    "        while i<j:\n",
    "            if a[i] in v and a[j] in v: a[i],a[j],i,j = a[j],a[i],i+1,j-1 #both letters are vowels, reasign a[i] with a[j], also increment i, decrement j\n",
    "            elif a[j] in v: i+=1 #only a[j] are vowel then we need increment i\n",
    "            else: j-=1 #in other case decrement j\n",
    "        return \"\".join(a)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        vowels = 'aeiouAEIOU'\n",
    "        vs = ''\n",
    "        for x in s:\n",
    "            if x in vowels:\n",
    "                vs += x\n",
    "        j = len(vs) - 1\n",
    "        res = ''\n",
    "        for x in s:\n",
    "            if x in vowels:\n",
    "                res += vs[j]\n",
    "                j -= 1\n",
    "            else:\n",
    "                res += x\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        t = \"\"\n",
    "        for c in s:\n",
    "            if c.lower() in \"aeiou\":\n",
    "                t += c\n",
    "        t = t[::-1]\n",
    "        idx = 0\n",
    "        res = \"\"\n",
    "        for c in s:\n",
    "            if c.lower() in \"aeiou\":\n",
    "                res += t[idx]\n",
    "                idx += 1\n",
    "            else:\n",
    "                res += c\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        lst=['a','e','i','o','u','A','E','I','O','U']\n",
    "        lst1=[]\n",
    "        str1=''\n",
    "        for i in s:\n",
    "            if i in lst:\n",
    "                lst1.append(i)\n",
    "        lst3=lst1[::-1]\n",
    "        k=0\n",
    "        for i in s:\n",
    "            if i not in lst:\n",
    "                str1=str1+i\n",
    "            else:\n",
    "                str1=str1+lst3[k]\n",
    "                k+=1\n",
    "        return str1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        i = 0\n",
    "        j = len(s) - 1\n",
    "        s_list = list(s)\n",
    "        vowels={'a', 'e', 'i', 'o', 'u','A','E','I','O','U'}\n",
    "        while i < j:\n",
    "            if s_list[i] in vowels and s_list[j] in vowels:\n",
    "                s_list[i], s_list[j] = s_list[j], s_list[i]\n",
    "                i += 1\n",
    "                j -= 1\n",
    "            while i<j and s_list[i] not in vowels:\n",
    "                i += 1\n",
    "            while i<j and  s_list[j] not in vowels:\n",
    "                j -= 1\n",
    "        return \"\".join(s_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: 'str') -> 'str':\n",
    "        vowels = set('aeiouAEIOU')\n",
    "        s = list(s)\n",
    "        n = len(s)\n",
    "        i, j = 0, n - 1\n",
    "        while i < j:\n",
    "            if s[i] not in vowels:\n",
    "                i += 1\n",
    "            elif s[j] not in vowels:\n",
    "                j -= 1\n",
    "            else:\n",
    "                s[i], s[j] = s[j], s[i]\n",
    "                i, j = i + 1, j - 1\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        l = []\n",
    "        ans = []\n",
    "        vowels = set('aeiouAEIOU')\n",
    "        for i in s:\n",
    "            if i in vowels:\n",
    "                l.append(i)\n",
    "        for i in s:\n",
    "            if i in vowels:\n",
    "                ans.append(l.pop())\n",
    "            else:\n",
    "                ans.append(i)\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}\n",
    "        L = list(s)\n",
    "        i, j = 0, len(L) - 1\n",
    "        while i < j:\n",
    "            while i < j and L[i] not in vowels:\n",
    "                i += 1\n",
    "            while j > i and L[j] not in vowels:\n",
    "                j -= 1\n",
    "            L[i], L[j] = L[j], L[i]\n",
    "            i += 1\n",
    "            j -= 1\n",
    "        return ''.join(L)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        if not s:\n",
    "            return s\n",
    "        yuanyin = ['a','e','i','o','u','A','E','I','O','U']\n",
    "        i = 0 \n",
    "        j = len(s) - 1\n",
    "        s = list(s)\n",
    "        while i < j:\n",
    "            if s[i] not in yuanyin and s[j] not in yuanyin:\n",
    "                i += 1\n",
    "                j -= 1\n",
    "            elif s[i] in yuanyin and s[j] not in yuanyin:\n",
    "                j -= 1\n",
    "            elif s[i] not in yuanyin and s[j] in yuanyin:\n",
    "                i += 1\n",
    "            else:\n",
    "                s[i], s[j] = s[j], s[i]\n",
    "                i += 1\n",
    "                j -= 1\n",
    "        return \"\".join(str(i) for i in s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        vowelsSet = set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'])\n",
    "        ans, vowelsPos = [], []\n",
    "        for i in range(len(s)):\n",
    "            ans.append(s[i])\n",
    "            if s[i] in vowelsSet:\n",
    "                vowelsPos.append(i)\n",
    "        i, j = 0, len(vowelsPos) - 1\n",
    "        while i < j:\n",
    "            ans[vowelsPos[i]], ans[vowelsPos[j]] = s[vowelsPos[j]], s[vowelsPos[i]]\n",
    "            i, j = i + 1, j - 1\n",
    "        return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: 'str') -> 'str':\n",
    "        l = [\"a\",\"e\",\"i\",\"o\",\"u\",\"A\",\"E\",\"I\",\"O\",\"U\"]\n",
    "        res = list(s)\n",
    "        l_index = []\n",
    "        for i in range(len(res)):\n",
    "            if res[i] in l:\n",
    "                l_index.append(i)\n",
    "        for j in range(len(l_index) // 2):\n",
    "            res[l_index[j]], res[l_index[-j-1]] =  res[l_index[-j-1]], res[l_index[j]]\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        s=list(s)\n",
    "        s1=list('aeiouAEIOU')\n",
    "        s2=[]\n",
    "        s3=[]\n",
    "        for i in range(len(s)):\n",
    "            if s[i] in s1:\n",
    "                s2.append(i)\n",
    "                s3.append(s[i])\n",
    "        s3=s3[::-1]\n",
    "        for j in range(len(s2)):\n",
    "            s[s2.pop()]=s3.pop()\n",
    "        return ''.join(s)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: 'str') -> 'str':\n",
    "        \"\"\"\n",
    "        二十六个拉丁字母中的五个元音字母是aeiou双指针，从头尾开始比较\n",
    "        \"\"\"\n",
    "        y_list = ['a','e','i','o','u']\n",
    "        s_list = list(s)\n",
    "        start = 0\n",
    "        end = len(s)-1\n",
    "        print(len(s))\n",
    "        while start < end:\n",
    "            if s_list[start].lower() not in y_list:  # 头\n",
    "                start += 1\n",
    "            if s_list[end].lower() not in y_list:  # 尾\n",
    "                end -= 1\n",
    "            if s_list[start].lower() in y_list and s_list[end].lower() in y_list:\n",
    "                print(s_list[start])\n",
    "                print(s_list[end])\n",
    "                s_list[start],s_list[end] = s_list[end],s_list[start]\n",
    "                start += 1\n",
    "                end -= 1\n",
    "        print(\"\".join(s_list))\n",
    "        return \"\".join(s_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        string = []\n",
    "        target = 'aeiouAEIOU'\n",
    "        temp = []\n",
    "        index = []\n",
    "        for x in range(len(s)):\n",
    "            string.append(s[x])\n",
    "            if s[x] in target:\n",
    "                temp.append(s[x])\n",
    "                index.append(x)\n",
    "        temp[:] = temp[::-1]\n",
    "        for x in range(len(index)):\n",
    "            string[index[x]] = temp[x]\n",
    "            \n",
    "        return ''.join(string)\n",
    "                \n",
    "            \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        alpha='aeouiAEOUI'\n",
    "        list1=list(s)\n",
    "        list2=list1.copy()\n",
    "        listindex=[index for index in range(len(list1)) if list1[index] in alpha]\n",
    "        listcopy=listindex.copy()\n",
    "        listindex.reverse()\n",
    "        for i in range(len(listindex)):\n",
    "            list2[listcopy[i]]=list1[listindex[i]]\n",
    "        return ''.join(list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        vowelSet = ('aeiouAEIOU')\n",
    "        vowelIdx = []\n",
    "        newStrArray = []\n",
    "        for i, c in enumerate(s):\n",
    "            newStrArray.append(c)\n",
    "            if c in vowelSet:\n",
    "                vowelIdx.append(i)\n",
    "        \n",
    "        \n",
    "        left = 0\n",
    "        right = len(vowelIdx) - 1\n",
    "        while left < right:\n",
    "            newStrArray[vowelIdx[left]], newStrArray[vowelIdx[right]] = newStrArray[vowelIdx[right]], newStrArray[vowelIdx[left]] # swap\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        \n",
    "        return ''.join(newStrArray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        letters = 'aeiouAEIOU'\n",
    "        s = list(s)\n",
    "        obs = {}\n",
    "        idxs = []\n",
    "        for i, e in enumerate(s):\n",
    "            if e in letters:\n",
    "                obs[i] = e\n",
    "                idxs.append(i)\n",
    "        \n",
    "        l = len(idxs)\n",
    "        if l > 1:\n",
    "            for i in range(l//2):\n",
    "                s[idxs[i]] = obs[idxs[l-1-i]]\n",
    "                s[idxs[l-1-i]] = obs[idxs[i]]\n",
    "        \n",
    "        return ''.join(s)\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        l = list(s)\n",
    "        index = []\n",
    "        item = []\n",
    "        for i in range(len(l)):\n",
    "            if l[i].lower() in [\"a\",\"e\",\"i\",\"o\",\"u\"]:\n",
    "                index.append(i)\n",
    "                item.append(l[i])\n",
    "        item.reverse()\n",
    "        mapper = { k:v for k,v in zip(index,item)}\n",
    "        for k,v in mapper.items():\n",
    "            l[k] = v\n",
    "        return \"\".join(l)\n",
    "        \n",
    "        \n",
    "                \n",
    "                    \n",
    "                    \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        sets = set(['a', 'e', 'i', 'o', 'u', 'A','E','I','O','U'])\n",
    "        l = []\n",
    "        for c in s:\n",
    "            if c in sets:\n",
    "                l.append(c)\n",
    "        \n",
    "        r = []\n",
    "        for c in s:\n",
    "            if c in sets:\n",
    "                r.append(l[-1])\n",
    "                l = l[:-1]\n",
    "            else:\n",
    "                r.append(c)\n",
    "        return ''.join(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']\n",
    "        v = []\n",
    "        ans = ''\n",
    "        for i in s:\n",
    "            if i in vowels:\n",
    "                v.append(i)\n",
    "        for i in s:\n",
    "            if i in vowels:\n",
    "                ans += v.pop()\n",
    "            else:\n",
    "                ans += i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        vowels = set('aeiouAEIOU')\n",
    "        sl = [i for i in s if i in vowels]\n",
    "        r = ''\n",
    "        for i in s:\n",
    "            if i not in vowels:\n",
    "                r += i\n",
    "            else:\n",
    "                r += sl.pop()\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        # 入栈法，把元音入栈，遇到元音出栈\n",
    "        stack = []\n",
    "        for c in s:\n",
    "            if c in 'aeiouAEIOU':\n",
    "                stack.append(c)\n",
    "        res = ''\n",
    "        for c in s:\n",
    "            if c in 'aeiouAEIOU':\n",
    "                res += stack.pop()\n",
    "            else:\n",
    "                res += c\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        s=list(s)\n",
    "        yuan=['a','e','i','o','u','A','E','I','O','U']\n",
    "        left=0\n",
    "        right=len(s)-1\n",
    "        while(left<right):\n",
    "            if s[left] not in yuan:\n",
    "                left+=1\n",
    "                continue\n",
    "            if s[right] not in yuan:\n",
    "                right-=1\n",
    "                continue\n",
    "            s[left],s[right]=s[right],s[left]\n",
    "            left+=1\n",
    "            right-=1\n",
    "        return ''.join(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        aeiou = ''\n",
    "        for each in s:\n",
    "            if each in 'aeiouAEIOU':\n",
    "                aeiou += each\n",
    "        aeiou = aeiou[::-1]\n",
    "        i = 0\n",
    "        ans = ''\n",
    "        for each in s:\n",
    "            if each in 'aeiouAEIOU':\n",
    "                ans += aeiou[i]\n",
    "                i += 1\n",
    "            else:\n",
    "                ans += each\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        if len(s) <= 1:\n",
    "            return s\n",
    "        tmp = {'e','o','i','a','u', 'A','E','I','O','U' }\n",
    "        i = 0\n",
    "        N = len(s)\n",
    "        j = N-1\n",
    "        part1 = \"\"\n",
    "        part2 = \"\"\n",
    "        while i <= j:\n",
    "            if i == j:\n",
    "                part1 += s[i]\n",
    "                break\n",
    "            elif (s[i] in tmp) and (s[j] in tmp):\n",
    "                part1 += s[j]\n",
    "                part2 += s[i]\n",
    "                i += 1\n",
    "                j -= 1\n",
    "            elif (s[i] not in tmp) and (s[j] in tmp):\n",
    "                part1 += s[i]\n",
    "                i += 1 \n",
    "            elif (s[i] not in tmp) and (s[j] not in tmp):\n",
    "                part1 += s[i]\n",
    "                part2 += s[j]\n",
    "                i += 1\n",
    "                j -= 1\n",
    "            else :\n",
    "                part2 += s[j]\n",
    "                j -= 1\n",
    "\n",
    "        return part1 + part2[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "\n",
    "        letter = ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')\n",
    "        \n",
    "        s = list(s)\n",
    "        #print(s)\n",
    "        i, j = 0, len(s) - 1\n",
    "        while i < j:\n",
    "            #print(\"i, j\", i, j, s[i], s[j])\n",
    "            if s[i] not in letter:\n",
    "                i += 1\n",
    "\n",
    "            if s[j] not in letter:\n",
    "                j -= 1\n",
    "            #print(\"after move\", s[i], \"[\",s[j],\"]\")\n",
    "            if i<len(s) and j >=0 and s[i] in letter and s[j] in letter:\n",
    "                #print(\"need to switch\", s[i], s[j], s)\n",
    "                s[i], s[j] = s[j], s[i]\n",
    "                #print(\"after switch\", s)\n",
    "                i += 1\n",
    "                j -= 1\n",
    "\n",
    "        return \"\".join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "\n",
    "        ss = list(s)\n",
    "\n",
    "        aeiou = ['a', 'e', 'i', 'o', 'u', 'A','E','I','O','U']\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        j = n-1\n",
    "\n",
    "        while i < j:\n",
    "            if ss[i] not in aeiou:\n",
    "                i += 1\n",
    "                continue\n",
    "            if ss[j] not in aeiou:\n",
    "                j -= 1\n",
    "                continue\n",
    "            if (i < j):\n",
    "                ss[i], ss[j] = ss[j], ss[i]\n",
    "            i += 1\n",
    "            j -= 1\n",
    "        d = ''\n",
    "        return d.join(ss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s):\n",
    "        array = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']\n",
    "        sList = list(s)\n",
    "        left, right = 0, len(s)-1\n",
    "        while left < right:\n",
    "            if sList[left] in array and sList[right] in array:\n",
    "                sList[left], sList[right] = sList[right], sList[left]\n",
    "                left += 1\n",
    "                right -= 1\n",
    "            if sList[right] not in array:\n",
    "                right -= 1\n",
    "            if sList[left] not in array:\n",
    "                left += 1\n",
    "        return ''.join(sList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        vowel = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']\n",
    "        s_vowel = []\n",
    "        for ch in s:\n",
    "            if ch in vowel:\n",
    "                s_vowel.insert(0, ch)\n",
    "\n",
    "        new_s = ''\n",
    "        i = 0\n",
    "        for ch2 in s:\n",
    "            if ch2 not in vowel:\n",
    "                new_s += ch2\n",
    "            else:\n",
    "                new_s += s_vowel[i]\n",
    "                i += 1\n",
    "        return new_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        # target = \"aeiouAEIOU\"\n",
    "        # s = list(s)\n",
    "        # l = len(s)\n",
    "        # idx = []\n",
    "        # ss = \"\"\n",
    "        # for i in range(l):\n",
    "        #     if s[i] in target:\n",
    "        #         idx.append(i)\n",
    "        #         ss+=s[i]\n",
    "        # ss = ss[::-1]\n",
    "        # for i in range(len(ss)):\n",
    "        #     s[idx[i]] = ss[i]\n",
    "        # return \"\".join(s)\n",
    "        target = \"aeiouAEIOU\"\n",
    "        res = \"\"\n",
    "        tail = len(s)-1\n",
    "        for i in range(len(s)):\n",
    "            if s[i] in target:\n",
    "                while s[tail] not in target:\n",
    "                    tail-=1\n",
    "                res+=s[tail]\n",
    "                tail-=1\n",
    "            else:\n",
    "                res+=s[i]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        alpha = ''\n",
    "        for a in s:\n",
    "            if a in ['a','e','i','o','u','A','E','I','O','U']:\n",
    "                alpha = a + alpha\n",
    "        res = ''\n",
    "        count  = 0\n",
    "        for a in s:\n",
    "            if a in ['a','e','i','o','u','A','E','I','O','U']:\n",
    "                res += alpha[count]\n",
    "                count += 1\n",
    "            else:\n",
    "                res += a\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        yuan_yin = {'a','e','i','o','u','A','E','I','O','U'}\n",
    "        l = 0\n",
    "        r = len(s)-1\n",
    "        if l==r:\n",
    "            return s\n",
    "        ansl = ''\n",
    "        ansr = ''\n",
    "        while l<=r:\n",
    "            while l<r and s[l] not in yuan_yin:\n",
    "                ansl = ansl + s[l]\n",
    "                l+=1\n",
    "            while l<r and s[r] not in yuan_yin:\n",
    "                ansr = s[r] + ansr\n",
    "                r-=1\n",
    "            if l<r:\n",
    "                ansl = ansl + s[r]\n",
    "                ansr = s[l] + ansr\n",
    "                l+=1\n",
    "                r-=1\n",
    "            elif l==r:\n",
    "                ansl = ansl + s[l]\n",
    "                break\n",
    "        return ansl+ansr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        yuan = ['a','e','i','o','u','A','E','I','O','U']\n",
    "        nor = \"\"\n",
    "        yuan_str = []\n",
    "        for i in s:\n",
    "            if i in yuan:\n",
    "                yuan_str.append(i)  \n",
    "        for i in s:\n",
    "            if i not in yuan:\n",
    "                nor += i  \n",
    "            else:\n",
    "                nor += yuan_str.pop()\n",
    "        return nor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        dic = {'a','e','i','o','u','A','E','I','O','U'} # 大小写的元音字母作为判断的依据\n",
    "        left = 0\n",
    "        right = len(s) - 1\n",
    "        s_ = list(s) # str类型是 可以 直接查询 in 和 not in的，转换为 list 只是为了方便修改数据，因为在python中，str类型是不可变的\n",
    "        while left < right : # 左右指针交错，循环停止\n",
    "            if s_[left] in dic and s_[right] in dic: # 左右指针所指元素均在集合中\n",
    "                s_[left] , s_[right] = s_[right] , s_[left] # 交换左右指针所指元素\n",
    "                left += 1  # 左指针右移\n",
    "                right -= 1 # 右指针左移\n",
    "            if s_[left] not in dic: # 左指针所指元素不在集合中\n",
    "                left += 1  # 左指针右移\n",
    "            if s_[right] not in dic: # 右指针所指元素不在集合中\n",
    "                right -= 1 # 右指针左移\n",
    "        return ''.join(s_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        lenth =len(s)\n",
    "        num=['a','e','i','o','u','A','E','I','O','U']\n",
    "        val=[]\n",
    "        p=''\n",
    "        for n,v in enumerate(s):\n",
    "            if v in num:\n",
    "                val.append(v)\n",
    "        for i in range(lenth):\n",
    "            if s[i] in num:\n",
    "                a = val.pop()\n",
    "                p+=a\n",
    "            else:\n",
    "                p+=s[i]\n",
    "        return p\n",
    "                \n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        h = set('aeiouAEIOU')\n",
    "        i=0\n",
    "        j=len(s)-1\n",
    "        while(i<j):\n",
    "            while(True):\n",
    "                if s[i] not in h and i<j:\n",
    "                    i=i+1\n",
    "                else:\n",
    "                    break\n",
    "            while(True):\n",
    "                if s[j] not in h and j>=0:\n",
    "                    j=j-1\n",
    "                else:\n",
    "                    break\n",
    "            if i>=j:\n",
    "                break\n",
    "            temp=s[i]\n",
    "            s=s[:i]+s[j]+s[i+1:]\n",
    "            s=s[:j]+temp+s[j+1:]\n",
    "            i=i+1\n",
    "            j=j-1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        j = len(s) - 1\n",
    "        ans = \"\"\n",
    "        d = {'a', 'e', 'i', 'o', 'u'}\n",
    "        for c in s:\n",
    "            if c.lower() not in d:\n",
    "                ans += c\n",
    "                continue\n",
    "            while s[j].lower() not in d:\n",
    "                j -= 1\n",
    "            ans += s[j]\n",
    "            j -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        l, r = 0, len(s) - 1\n",
    "        temp = [\"a\", \"e\", \"i\", \"o\",\"u\"]\n",
    "        cur = []\n",
    "        for i in s:\n",
    "            cur.append(i)\n",
    "        while l < r:\n",
    "            if s[l].lower() not in temp:\n",
    "                l += 1\n",
    "            if s[r].lower() not in temp:\n",
    "                r -= 1\n",
    "            if s[l].lower() in temp and s[r].lower() in temp:\n",
    "                t = cur[l]\n",
    "                cur[l] = cur[r]\n",
    "                cur[r] = t\n",
    "                l += 1\n",
    "                r -= 1\n",
    "        return \"\".join(cur)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        words = list(s)\n",
    "        left, right = 0, len(words) - 1\n",
    "        y = ['a', 'i','e','o','u','A','E','I','O','U']\n",
    "        while left < right:\n",
    "            while left <= right and left < len(words)  and words[left] not in y :\n",
    "                left += 1\n",
    "            while right >= left and right >= 0 and words[right] not in y  :\n",
    "                right -= 1\n",
    "            if left < len(words) and right >= 0  and words[left] in y and words[right] in y:\n",
    "                words[left],words[right] = words[right], words[left]\n",
    "            left += 1\n",
    "            right -= 1\n",
    "\n",
    "        return \"\".join(words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        i,j=0,len(s)-1\n",
    "        lst=[*s]\n",
    "        yuanyin=[*'aeiouAEIOU']\n",
    "        while i<j:\n",
    "            if lst[i] in yuanyin and lst[j] in yuanyin:\n",
    "                lst[i],lst[j]=lst[j],lst[i]\n",
    "                i+=1\n",
    "                j-=1\n",
    "            elif lst[i] not in yuanyin:\n",
    "                i+=1\n",
    "            elif lst[j] not in yuanyin:\n",
    "                j-=1\n",
    "        return ''.join(lst)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        s2 = []\n",
    "        for i in s:\n",
    "            if (i in \"aeiouAEIOU\"):\n",
    "                s2.append(i)\n",
    "        s2 = s2[::-1]\n",
    "        j = 0 \n",
    "        for i in range(len(s)):\n",
    "            if s[i] in \"aeiouAEIOU\":\n",
    "                s[i]= s2[j]\n",
    "                j = j + 1\n",
    "        return \"\".join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        aaa = [\"a\",\"e\",\"i\",\"o\",\"u\",\"A\",\"E\",\"I\",\"O\",\"U\"]\n",
    "        t = list(s)\n",
    "        i = 0\n",
    "        j = len(t)-1 \n",
    "        while(i<j):\n",
    "            if t[i] in aaa and t[j] in aaa:\n",
    "                a = t[i]\n",
    "                t[i] = t[j]\n",
    "                t[j] = a\n",
    "                i+=1\n",
    "                j-=1\n",
    "            else:\n",
    "                if  t[i] not in aaa:\n",
    "                    i+=1\n",
    "                if  t[j] not in aaa:\n",
    "                    j-=1   \n",
    "        print(t)       \n",
    "        t = \"\".join(t)\n",
    "        return t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        aaa = [\"a\",\"e\",\"i\",\"o\",\"u\",\"A\",\"E\",\"I\",\"O\",\"U\"]\n",
    "        t = list(s)\n",
    "        i = 0\n",
    "        j = len(t)-1 \n",
    "        while(i<j):\n",
    "            if t[i] in aaa and t[j] in aaa:\n",
    "                a = t[i]\n",
    "                t[i] = t[j]\n",
    "                t[j] = a\n",
    "                i+=1\n",
    "                j-=1\n",
    "            else:\n",
    "                if  t[i] not in aaa:\n",
    "                    i+=1\n",
    "                if  t[j] not in aaa:\n",
    "                    j-=1   \n",
    "        print(t)       \n",
    "        l = \"\".join(t)\n",
    "        return l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        vs = [\"a\",\"e\",\"i\",\"o\",\"u\",\"A\",\"E\",\"I\",\"O\",\"U\"]\n",
    "        indexs = []\n",
    "        s1 = []\n",
    "        for i in range(len(s)):\n",
    "            if s[i] in vs:\n",
    "                indexs.append(i)\n",
    "            s1.append(s[i])\n",
    "        n = len(indexs)\n",
    "        if n > 1:\n",
    "            for j in range(n//2):\n",
    "                t = s1[indexs[j]]\n",
    "                s1[indexs[j]] = s1[indexs[n-1-j]]\n",
    "                s1[indexs[n-1-j]] = t\n",
    "        return ''.join(s1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reverseVowels(self, s: str) -> str:\n",
    "        mem = []\n",
    "        mapping = {\"a\", \"e\", \"i\", \"o\", \"u\", \"A\", \"E\", \"I\", \"O\", \"U\"}\n",
    "\n",
    "        s_list = list(s)\n",
    "        for i, cs in enumerate(s_list):\n",
    "            if cs in mapping:\n",
    "                mem.append(i)\n",
    "\n",
    "        left, right = 0, len(mem) - 1\n",
    "        while left < right:\n",
    "            s_list[mem[left]], s_list[mem[right]] = s_list[mem[right]], s_list[mem[left]]\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return ''.join(s_list)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
